using System;
using System.Collections.Generic;
using System.Reflection;

namespace Serpis
{
	internal class PersisterInfo
	{
//		private static string parameterPrefix = AppDbConnection.Instance.ParameterPrefix;

		public PersisterInfo (Type type)
		{
			this.type = type;
			tableName = type.Name.ToLower();
			
			List<PropertyInfo> fieldList = new List<PropertyInfo>();
			List<string> fieldNameList = new List<string>();
			
			foreach (PropertyInfo propertyInfo in type.GetProperties()) {
				if (propertyInfo.IsDefined(typeof(PrimaryKeyAttribute), true)) {
					primaryKey = propertyInfo;
					primaryKeyName = propertyInfo.Name.ToLower();
					continue;
				}
				if (propertyInfo.IsDefined(typeof(FieldAttribute), true)) {
					fieldList.Add(propertyInfo);
					fieldNameList.Add(propertyInfo.Name.ToLower());
				}
			}
			
			fields = fieldList.ToArray();
			fieldNames = fieldNameList.ToArray();
			
//			makeCommands();			
		}
		
//		private void makeCommands()
//		{
//			//"insert into articulo (nombre, precio, categoria) values (?nombre, ?precio, ?categoria)";
//			//"update articulo set nombre=?nombre, precio=?precio, categoria=?categoria where id=?id";
//			//"select * from articulo where id=?id";
//			string[] parameters = new string[fieldNames.Length];
//			for (int index = 0; index < fieldNames.Length; index++)
//				parameters[index] = parameterPrefix + fieldNames[index];
//			
//			insertCommand = string.Format("insert into {0} ({1}) values ({2})", 
//				tableName, string.Join(",", fieldNames), string.Join(",", parameters));
//			
//			string[] updateList = new string[fieldNames.Length];
//			for (int index = 0; index < fieldNames.Length; index++)
//				updateList[index] = string.Format("{0}={1}", fieldNames[index], parameters[index]);
//			
//			updateCommand = string.Format("update {0} set {1} where {2}={3}{2}",
//			    tableName, string.Join(",", updateList), primaryKeyName, parameterPrefix);
//			
//			selectCommand = string.Format("select * from {0} where {1}={2}{1}",
//				tableName, primaryKeyName, parameterPrefix);
//			
//			deleteCommand = string.Format("delete from {0} where {1}={2}{1}",
//				tableName, primaryKeyName, parameterPrefix);
//		}
		
		private Type type;
		private string tableName;
		private string primaryKeyName;
		private string[] fieldNames;
		private PropertyInfo primaryKey;
		private PropertyInfo[] fields;
		
		private string insertCommand;
		private string updateCommand;
		private string selectCommand;
		private string deleteCommand;
		
		public Type Type {
			get {return type;}
		}
		
		public string TableName {
			get {return tableName;}
		}
		
		
		public string PrimaryKeyName {
			get {return primaryKeyName;}
		}
		
		
		public PropertyInfo PrimaryKey {
			get {return primaryKey;}
		}
		
		
		public PropertyInfo[] Fields {
			get {return fields;}
		}
		
		
		public string[] FieldNames {
			get {return fieldNames;}
		}
		
		public string UpdateCommand {
			get {return updateCommand;}
		}
		
		
		public string SelectCommand {
			get {return selectCommand;}
		}
		
		
		public string InsertCommand {
			get {return insertCommand;}
		}
		
		public string DeleteCommand {
			get {return deleteCommand;}
		}
	}
}
